package socketdemo3;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/*
 * 服务端
 * 1.获取客户端的socket对象
 * 2.通过socket对象获取客户端的输入流
 * 3.创建服务端的输出流
 * */
public class Server {
    /**
     * 服务器serversocket对象
     */
    private ServerSocket serverSocket;
    private List<PrintWriter> allOut = new ArrayList<>();

    public Server() {
        try {
            //参数：服务端要监听的端口号
            serverSocket = new ServerSocket(8088);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 作为整个程序的开始方法使用
     */
    private void start() {
        try {
            while (true) {
                //等待客户端的链接
                Socket socket = serverSocket.accept();
                //创建多线程实例
                Runnable r = new multilink(socket);
                Thread t = new Thread(r);
                //启动多线程
                t.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }

    /*用于实现多个客户端可以链接服务端*/
    private class multilink implements Runnable {

        private Socket socket;

        public multilink(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            PrintWriter pw = null;
            try {
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);

                OutputStream os = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
                BufferedWriter bw = new BufferedWriter(osw);
                pw = new PrintWriter(bw, true);

                //将每个客户端的输出流分别放在allOut数组当中
                synchronized (allOut) {
                    allOut.add(pw);
//                System.out.println(allOut.toArray());
                    RadioBroadcast("当前在线人数:" + allOut.size());
                }

                String line;
                //当客服端输入的信息不等于null时循环条件成立继续循环
                while ((line = br.readLine()) != null) {
                    RadioBroadcast("服务端返回:" + line);
                }

            } catch (IOException e) {

            } finally {
                try {
                    synchronized (allOut) {
                        allOut.remove(pw);
                        RadioBroadcast("当前在线人数" + allOut.size());
                        socket.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        private void RadioBroadcast(String line) {
            synchronized (allOut) {
                for (PrintWriter p : allOut) {
                    p.println(line);
                }
            }
        }

    }

}
